package com.jiamingZhao.enums;

import lombok.Getter;

import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

@Getter
public enum FiledType {




    STRING(String.class) {
        @Override
        public Object getResultValue(ResultSet rs, Class<?> fieldType, String columnName) {
            try {
                return rs.getString(columnName);
            } catch (SQLException e) {
                return null;
            }
        }
    },
    INTEGER(Integer.class) {
        @Override
        public Object getResultValue(ResultSet rs, Class<?> fieldType, String columnName) {
            try {
                return rs.getInt(columnName);
            } catch (SQLException e) {
                return null;
            }
        }
    },
    LONG(Long.class) {
        @Override
        public Object getResultValue(ResultSet rs, Class<?> fieldType, String columnName) {
            try {
                return rs.getLong(columnName);
            } catch (SQLException e) {
                return null;
            }
        }
    },
    DOUBLE(Double.class) {
        @Override
        public Object getResultValue(ResultSet rs, Class<?> fieldType, String columnName) {
            try {
                return rs.getDouble(columnName);
            } catch (SQLException e) {
                return null;
            }
        }
    },
    FLOAT(Float.class) {
        @Override
        public Object getResultValue(ResultSet rs, Class<?> fieldType, String columnName) {
            try {
                return rs.getFloat(columnName);
            } catch (SQLException e) {
                return null;
            }
        }
    },
    BOOLEAN(Boolean.class) {
        @Override
        public Object getResultValue(ResultSet rs, Class<?> fieldType, String columnName) {
            try {
                return rs.getBoolean(columnName);
            } catch (SQLException e) {
                return null;
            }
        }
    },
    DATE(java.util.Date.class) {
        @Override
        public Object getResultValue(ResultSet rs, Class<?> fieldType, String columnName) {
            try {
                return rs.getDate(columnName);
            } catch (SQLException e) {
                return null;
            }
        }
    };


    private final Class<?> objectType;

    FiledType(Class<?> objectType) {
        this.objectType = objectType;
    }

    public abstract Object getResultValue(ResultSet rs, Class<?> fieldType, String columnName);



    public static void fillEntityFiled(Object entity, Map<String, Method> methodsMap, ResultSet rs) {
        methodsMap.keySet().forEach(e -> {
            Method m  = methodsMap.get(e);
            try {
                Class<?> type = m.getParameters()[0].getType();
                FiledType fileType = FiledType.getFiledType(type);
                if (fileType != null) {
                    m.invoke(entity, fileType.getResultValue(rs, type, e));
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
    }
    public static FiledType getFiledType(Class<?> fieldType) {
        for (FiledType type : FiledType.values()) {
            if (type.getObjectType().equals(fieldType)) {
                return type;
            }
        }
        return null;
    }
}
